// pages/menu/deviceList/deviceDetail/vending-aisle/aisle-detail/aisle-detail.js
var verify = require('../../../../../../utils/verify-token.js')
var app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {

	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		//console.log(options);
		var deviceId = options.deviceId;
		var deviceGoodsAisleId = options.deviceGoodsAisleId;
		this.setData({
			sh: app.globalData.screenHeight,
			sw: app.globalData.screenWidth,
			deviceId: deviceId,
			deviceGoodsAisleId: deviceGoodsAisleId,
			aisleDetail: {}
		});
		this.getDetail();
	},
	//获取详情
	getDetail(){
		let that = this;
		let deviceId = that.data.deviceId;
		let deviceGoodsAisleId = that.data.deviceGoodsAisleId;
		wx.request({
			url: app.globalData.baseUrl + 'vending/device/' + deviceId + '/goods_aisle/info?deviceGoodsAisleId=' + deviceGoodsAisleId,
			header: {
				'AUTHORIZATION':app.globalData.token,				
			},
			method: 'get',
			success(res) {
				if (res.data.code == 60006) {
					verify.clearAll()
				} else if (res.data.code == 200) {
					let aisleDetail = res.data.data;
					that.setData({
						aisleDetail:aisleDetail
					});
				} else {
					wx.showModal({
						title: '操作失败',
						content: res.data.message,
					})
				}
			}
		})
	},
	//启用/停用货道
	changeUsedStatus() {
		let that = this;
		let deviceId = that.data.deviceId;
		let deviceGoodsAisleId = that.data.deviceGoodsAisleId;
		wx.request({
			url: app.globalData.baseUrl + 'vending/device/' + deviceId + '/enabled/' + deviceGoodsAisleId,
			header: {
				'AUTHORIZATION': app.globalData.token,
				'content-type': 'application/x-www-form-urlencoded'
			},
			method: 'post',
			success(res) {
				if (res.data.code == 60006) {
					verify.clearAll()
				} else if (res.data.code == 200) {
					wx.showToast({
						title: '成功',
						icon: 'success',
						duration: 2000
					})					
					that.getDetail();
				} else {
					wx.showModal({
						title: '操作失败',
						content: res.data.message,
					})
				}
			}
		})
	},
	//出货
	outGoods() {
		let that = this;
		let deviceId = that.data.deviceId;
		let deviceGoodsAisleId = that.data.deviceGoodsAisleId;
		wx.request({
			url: app.globalData.baseUrl + 'vending/device/' + deviceId + '/sent_out/' + deviceGoodsAisleId,
			header: {
				'AUTHORIZATION': app.globalData.token,
				'content-type': 'application/x-www-form-urlencoded'
			},
			method: 'post',
			success(res) {
				if (res.data.code == 60006) {
					verify.clearAll()
				} else if (res.data.code == 200) {
					wx.showToast({
						title: '成功',
						icon: 'success',
						duration: 2000
					})					
					that.getDetail();
				} else {
					wx.showModal({
						title: '操作失败',
						content: res.data.message,
					})
				}
			}
		})
	},
	//编辑容量
	editShowVolume(e){		
		this.data.aisleDetail.volume = e.detail.value;
		let aisleDetail = this.data.aisleDetail;
		this.setData({
			aisleDetail:aisleDetail
		});
	},
	//编辑显示编号
	editNumber(e){
		this.data.aisleDetail.number = e.detail.value;
		let aisleDetail = this.data.aisleDetail;
		this.setData({
			aisleDetail:aisleDetail
		})
	},
	//点击操作
	actionComfirm(res){
		let that = this;
		let tag = res.currentTarget.dataset.tag;
		//console.log(tag);
		wx.showModal({
			title:'提示',
			content:'您确定要进行此操作吗？',
			success(res){
				if (res.confirm) {
					switch(tag){
						case 'save':
							that.comfirmModity();
							break;
						case 'outgoods':
							that.outGoods();
							break;
						case 'changeus':
							that.changeUsedStatus();
							break;
						case 'unmerged':
							that.unmerged();
							break;
					}
				} else if (res.cancel) {
					console.log('用户点击取消')
				}
			}
		})
	},
	//确定保存
	comfirmModity(){
		let that = this;
		let deviceId = that.data.deviceId;
		let deviceGoodsAisleId = that.data.deviceGoodsAisleId;
		let number = that.data.aisleDetail.number;
		let volume = that.data.aisleDetail.volume;
		let param = {
			deviceId:deviceId,
			deviceGoodsAisleId:deviceGoodsAisleId,
			volume:volume,
			number:number
		};
		wx.request({
			url: app.globalData.baseUrl + 'vending/device/' + deviceId + '/goods_aisle/update',
			header: {
				'AUTHORIZATION': app.globalData.token,
				'content-type': 'application/x-www-form-urlencoded'
			},
			method: 'post',
			data:param,
			success(res) {
				if (res.data.code == 60006) {
					verify.clearAll()
				} else if (res.data.code == 200) {
					wx.showToast({
						title: '成功',
						icon: 'success',
						duration: 2000
					})					
					that.getDetail();
				} else {
					wx.showModal({
						title: '操作失败',
						content: res.data.message,
					})
				}
			}
		})
	},
	//合并
	merged() {
		let deviceId = this.data.deviceId;
		let deviceGoodsAisleId = this.data.deviceGoodsAisleId;
		wx.navigateTo({
			url: 'merged-aisle/merged-aisle?deviceId=' + deviceId + '&deviceGoodsAisleId=' + deviceGoodsAisleId,
		})
	},
	//解除合并
	unmerged() {
		let that = this;
		let deviceId = that.data.deviceId;
		let deviceGoodsAisleId = that.data.deviceGoodsAisleId;
		wx.request({
			url: app.globalData.baseUrl + 'vending/device/' + deviceId + '/relieve_merged/' + deviceGoodsAisleId,
			header: {
				'AUTHORIZATION': app.globalData.token,
				'content-type': 'application/x-www-form-urlencoded'
			},
			method: 'put',
			success(res) {
				if (res.data.code == 60006) {
					verify.clearAll()
				} else if (res.data.code == 200) {
					wx.showToast({
						title: '成功',
						icon: 'success',
						duration: 2000
					})					
					that.getDetail();
				} else {
					wx.showModal({
						title: '操作失败',
						content: res.data.message,
					})
				}
			}
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
